package dhm.com.hpx.adapter;

import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import dhm.com.hpx.R;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.entity.GetUserVisitor;

/**
 * Created by admin
 * 2019/4/29
 * 所有订单适配器
 */
public class VisitorAdapter extends RecyclerView.Adapter<VisitorAdapter.ViewHolder> {

    private Context context;


    List<GetUserVisitor.DataBean> list = new ArrayList<>();

    public VisitorAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<GetUserVisitor.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewHolder viewHolder;
        View view = LayoutInflater.from(context).inflate(R.layout.recy_visitor, null);
        viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {

        viewHolder.time.setText(list.get(i).getVisitor_time());

        switch (list.get(i).getBrowse_type()){
            case 1:
                //app端访问
                viewHolder.visiver.setText(list.get(i).getAddress()+list.get(i).getNickname()+"通过App名片访问了您");

                if (list.get(i).getUser_info()==null){
                    if (list.get(i).getNickname()==null||list.get(i).getNickname().equals("")){
                        viewHolder.name.setText("暂无权限");
                    }else {
                        viewHolder.name.setText(list.get(i).getNickname());
                    }
                    if (list.get(i).getHeadimgurl()==null||list.get(i).getHeadimgurl().equals("")){
                        viewHolder.image.setImageResource(R.mipmap.zwt);
                    }else {
                        viewHolder.image.setImageURI(list.get(i).getHeadimgurl());
                    }

                    if (list.get(i).getPhone() == null || list.get(i).getPhone().equals("")) {
                        viewHolder.phone.setText("未填写手机号");
                    } else {
                        viewHolder.phone.setText(list.get(i).getPhone());
                    }
                }else {
                    viewHolder.image.setImageURI(Constant.PATH + list.get(i).getUser_info().getUserPhoto());
                    viewHolder.name.setText(list.get(i).getUser_info().getUserName());
                    if (list.get(i).getUser_info().getUserPhone() == null || list.get(i).getUser_info().getUserPhone().equals("")) {
                        viewHolder.phone.setText("未填写手机号");
                    } else {
                        viewHolder.phone.setText(list.get(i).getUser_info().getUserPhone());
                    }
                }


                break;
            case 2:
                //小程序端访问
                viewHolder.visiver.setText(list.get(i).getAddress()+list.get(i).getNickname()+"通过小程序名片访问了您");

                if (list.get(i).getUser_info()==null){
                    if (list.get(i).getNickname()==null||list.get(i).getNickname().equals("")){
                        viewHolder.name.setText("暂无权限");
                    }else {
                        viewHolder.name.setText(list.get(i).getNickname());
                    }
                    if (list.get(i).getHeadimgurl()==null||list.get(i).getHeadimgurl().equals("")){
                        viewHolder.image.setImageResource(R.mipmap.zwt);
                    }else {
                        viewHolder.image.setImageURI(list.get(i).getHeadimgurl());
                    }

                    if (list.get(i).getPhone() == null || list.get(i).getPhone().equals("")) {
                        viewHolder.phone.setText("未填写手机号");
                    } else {
                        viewHolder.phone.setText(list.get(i).getPhone());
                    }
                }else {
                    viewHolder.image.setImageURI(Constant.PATH + list.get(i).getUser_info().getUserPhoto());
                    viewHolder.name.setText(list.get(i).getUser_info().getUserName());
                    if (list.get(i).getUser_info().getUserPhone() == null || list.get(i).getUser_info().getUserPhone().equals("")) {
                        viewHolder.phone.setText("未填写手机号");
                    } else {
                        viewHolder.phone.setText(list.get(i).getUser_info().getUserPhone());
                    }
                }
                break;
            case 3:
                //浏览器端访问
                viewHolder.visiver.setText(list.get(i).getAddress()+list.get(i).getNickname()+"阅读了你分享的文章『 "+list.get(i).getTarget_title()+" 』");
                if (list.get(i).getNickname()==null||list.get(i).getNickname().equals("")){
                    viewHolder.name.setText("暂无权限");
                }else {
                    viewHolder.name.setText(list.get(i).getNickname());
                }
                if (list.get(i).getHeadimgurl()==null||list.get(i).getHeadimgurl().equals("")){
                    viewHolder.image.setImageResource(R.mipmap.zwt);
                }else {
                    viewHolder.image.setImageURI(list.get(i).getHeadimgurl());
                }

                if (list.get(i).getPhone() == null || list.get(i).getPhone().equals("")) {
                    viewHolder.phone.setText("未填写手机号");
                } else {
                    viewHolder.phone.setText(list.get(i).getPhone());
                }

                break;
                default:
        }

        viewHolder.wechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //复制微信号

                if (list.get(i).getUser_info()==null||list.get(i).getUser_info().getUserQQ() == null || list.get(i).getUser_info().getUserQQ().equals("")) {
                    Toast.makeText(context, "用户微信号暂未获取", Toast.LENGTH_SHORT).show();
                } else {
                    //获取剪贴板管理器：
                    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                    // 创建普通字符型ClipData
                    ClipData mClipData = ClipData.newPlainText("Label", list.get(i).getUser_info().getUserQQ());
                    // 将ClipData内容放到系统剪贴板里。
                    cm.setPrimaryClip(mClipData);
                    Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show();
                }

            }
        });

        viewHolder.call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 检查是否获得了权限（Android6.0运行时权限）
                //权限没有获得
                if (ContextCompat.checkSelfPermission(context,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // 没有获得授权，申请授权
                    if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                            Manifest.permission.CALL_PHONE)) {
                        // 返回值：
                        // 如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
                        // 如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
                        // 如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
                        // 弹窗需要解释为何需要该权限，再次请求授权
                        Toast.makeText(context, "请授权拨打电话权限！", Toast.LENGTH_LONG).show();

                        // 帮跳转到该应用的设置界面，让用户手动授权
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", context.getPackageName(), null);
                        intent.setData(uri);
                        context.startActivity(intent);
                    } else {
                        // 不需要解释为何需要该权限，直接请求授权
                        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, 100);
                    }
                } else {
                    // 已经获得授权，可以打电话
                    CallPhone(i);
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public SimpleDraweeView image;
        public TextView name;
        public TextView phone;
        public TextView visiver;
        public TextView call;
        public TextView wechat;
        public TextView time;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
            visiver = itemView.findViewById(R.id.visiver);
            call = itemView.findViewById(R.id.call);
            wechat = itemView.findViewById(R.id.wechat);
            time = itemView.findViewById(R.id.time);
        }
    }

    private OnClick onClick;

    public void setdeleteClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public interface OnClick {
        void item(String phone);
    }

    private OnIntentClick onIntentClick;

    public void setaddClick(OnIntentClick onClick) {
        this.onIntentClick = onClick;
    }

    public interface OnIntentClick {
        void item();
    }

    private void CallPhone(int i) {
        if ( list.get(i).getUser_info()==null){
            if (list.get(i).getPhone()!=null&&!list.get(i).getPhone().equals("")){
                String number = list.get(i).getPhone();
                if (number == null || number.equals("")) {
                    Toast.makeText(context, "用户未填写手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(number)) {
                    // 提醒用户
                    Toast.makeText(context, "用户未填写手机号码", Toast.LENGTH_SHORT).show();
                } else {
                    // 拨号：激活系统的拨号组件
                    Intent intent = new Intent(); // 意图对象：动作 + 数据
                    intent.setAction(Intent.ACTION_DIAL); // 设置动作
                    Uri data = Uri.parse("tel:" + list.get(i).getPhone()); // 设置数据
                    intent.setData(data);
                    context.startActivity(intent); // 激活Activity组件
                }
            }else {
                Toast.makeText(context, "用户未填写手机号码", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        String number = list.get(i).getUser_info().getUserPhone();
        if (number == null || number.equals("")) {
            Toast.makeText(context, "用户未填写手机号码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(number)) {
            // 提醒用户
            Toast.makeText(context, "用户未填写手机号码", Toast.LENGTH_SHORT).show();
        } else {
            // 拨号：激活系统的拨号组件
            Intent intent = new Intent(); // 意图对象：动作 + 数据
            intent.setAction(Intent.ACTION_DIAL); // 设置动作
            Uri data = Uri.parse("tel:" + number); // 设置数据
            intent.setData(data);
            context.startActivity(intent); // 激活Activity组件
        }
    }


}

